package manager.universe;

import javax.media.opengl.GL2;
import javax.media.opengl.GLContext;
import javax.media.opengl.glu.GLU;

import math.Quaternion;
import math.Vector3D;

import gui.GameFrame;

public class RenderManager extends UniverseManager{
	
	boolean test = false;
	GLU glu ;
	
	public RenderManager(int periode)
	{
		super(periode);
	}
	
	@Override
	protected void lockedRun(Universe u) {
		GL2 gl = (GL2) GameFrame.getInstance().getContext().getGL();
		double[] triangle = u.getTriangle();
			
		
		if(GameFrame.getInstance().requestedResize()){
			resizeView(gl);
			GameFrame.getInstance().setRequestResize(false);
		}
		
		
		
		
			gl.glMatrixMode(GL2.GL_PROJECTION);
			gl.glLoadIdentity();
			glu.gluPerspective(45.0, 1, 1.0, 20.0);
			
			u.getCamera().update();
			Vector3D position = u.getCamera().getPosition();
			Quaternion orientation = u.getCamera().getOrientation();
			double[] rot = orientation.quatToAxisAngle();
			if(rot[0]!=0){
			System.out.println("orientation2 = "+rot[0] +" "+rot[1] +" "+rot[2] +" "+rot[3]  );}
			
			
			glu.gluLookAt(position.getX(), position.getY(), position.getZ(), 0, 0, 0, 0, 0, 1); 


			
			

			
		gl.glMatrixMode(GL2.GL_MODELVIEW);
		       
		gl.glClear(GL2.GL_COLOR_BUFFER_BIT);

		

        gl.glLoadIdentity();
        gl.glTranslated(position.getX(), position.getY(), position.getZ());
        //gl.glMultMatrixf(orientation.quatToRot4D());
        //gl.glRotated( Math.toDegrees(rot[0]),rot[1],rot[2],rot[3]);
        gl.glTranslated(-position.getX(), -position.getY(),- position.getZ());
     

        
        gl.glBegin(GL2.GL_LINES);
        
        gl.glVertex2i(0,0);gl.glVertex2i(0,5);
        gl.glVertex2i(0,0);gl.glVertex2i(5,0);
        gl.glVertex2i(0,0);gl.glVertex3i(0,0,5);
        gl.glEnd();
        // draw a triangle filling the window
        gl.glBegin(GL2.GL_TRIANGLES);
        gl.glColor3f(1, 0, 0);
        //gl.glVertex2d(-triangle[2], -triangle[2]);
        gl.glVertex3d(0, 0,0);
        gl.glColor3f(0, 1, 0);
        //gl.glVertex2d(0, triangle[2]);
        gl.glVertex3d(0, 2,0);
        gl.glColor3f(0, 0, 1);
        //gl.glVertex2d(triangle[1], -triangle[1]);
        gl.glVertex3d(2, 0,0);
        gl.glEnd();
        GameFrame.getInstance().getDrawable().swapBuffers(); // put the scene onto the canvas
        // swap front and back buffers, making the rendering visible

	}
	
	protected void destroy(){
		GameFrame.getInstance().getContext().release();
		GameFrame.getInstance().getContext().destroy();
	}
	
	protected void init(){
		try {
			while (GameFrame.getInstance().getContext().makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) {
				System.out.println("Context not yet current...");
				Thread.sleep(100);
			}

			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//GL2 gl = (GL2) GameFrame.getInstance().getContext().getGL();
		glu = new GLU();
		//gl.glMatrixMode(GL2.GL_PROJECTION);
		//gl.glLoadIdentity();
		//glu.gluPerspective(45.0, 1, 1.0, 20.0);
		//glu.gluLookAt(10, 10, 10, 0, 0, 0, 0, 0, 1); 
		//gl.glMatrixMode(GL2.GL_MODELVIEW);
	
	}
	
	private void resizeView(GL2 gl) {
		gl.glViewport(0, 0, GameFrame.getInstance().getWidth(), GameFrame.getInstance().getHeight()); // drawing area
		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glLoadIdentity();
	}
}
